'''
循环输入10个人的信息（姓名，年龄，性别，身高），并存储到数据库中
'''
import pymysql
# 连接数据库
class Db_insert:
    def __init__(self,host,user,password,database,port=3306,charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset

    def Insert(self,sql):
       db = pymysql.connect(
          host=self.host,
          user=self.user,
          password=self.password,
          database=self.database,
          port=self.port,
          charset=self.charset
                          )
       try:
          with db.cursor() as cursor:
            cursor.execute(sql)
            db.commit()
            print('操作成功')
       except Exception as e:
            print('数据库异常', e)
       finally:
            db.close()

if __name__ == '__main__':
    num = 0
    while num <10:
        db = Db_insert('localhost','root','123456','practice')
        data = input('请输入姓名，年龄，性别，身高').split()
        name,age,sex,high = data
        sql = f'insert into person values({name},{age},{sex},{high})'
        db.Insert(sql)
        num+=1
    print('输入完成')




# # 创建游标
# curses = db.cursor()
# # 准备SQL语句
# sql = '''
# create  table  person (
#            username  varchar(20),
# 	       age       int,
# 	       sex 	  char(4),
# 	       high      double(3,2)
# )
# '''# 总共3位数字，2位小数
#sql = '''
                # create  table  person (
                # username  varchar(20),
    	        #  age       int,
    	        #  sex 	  char(4),
    	        #  high      double(3,2)
                #                         )'''
# # 执行sql语句
# cursos.execute(sql)
# db.commit()
# # 关闭游标
# cursos.close()
# # 关闭数据库
# db.close()





